<template>
  <div>
    <el-form
      :inline="true"
      size="small"
      ref="tableData"
      :model="tableData"
      class="demo-form-inline"
    >
      <el-form-item prop="id">
        <el-input
          v-model="tableData.id"
          clearable
          @clear="getMemberList"
          placeholder="会员ID"
        ></el-input>
      </el-form-item>
      <el-form-item prop="cardNum">
        <el-input v-model="tableData.cardNum" placeholder="会员卡号"></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="tableData.name" placeholder="会员名称"></el-input>
      </el-form-item>
      <el-form-item prop="payType">
        <el-select v-model="tableData.payType" placeholder="支付类型">
          <el-option
            v-for="(item, index) in payType"
            :key="index"
            :label="item.name"
            :value="item.type"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="birthday">
        <el-date-picker
          v-model="tableData.birthday"
          type="date"
          placeholder="出生日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="addDialog">新增</el-button>
        <el-button @click="resetting('tableData')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="memberList" border style="width: 100%">
      <el-table-column type="index" label="序号" width="60"> </el-table-column>
      <el-table-column prop="cardNum" label="会员卡号" width="180">
      </el-table-column>
      <el-table-column prop="name" label="会员名称"> </el-table-column>
      <el-table-column prop="birthday" label="会员生日" width="100">
      </el-table-column>
      <el-table-column prop="phone" label="手机号码" width="130">
      </el-table-column>
      <el-table-column prop="integral" label="可用积分"> </el-table-column>
      <el-table-column prop="money" label="开卡金额"> </el-table-column>
      <el-table-column prop="payType" label="支付类型">
        <template slot-scope="scope">
          {{ scope.row.payType | filterType }}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="会员地址" width="250">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="getDialog(scope.row.id)"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="delMember(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[7, 10, 15, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="450px">
      <span>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="会员卡号" prop="cardNum">
            <el-input v-model="ruleForm.cardNum"></el-input>
          </el-form-item>
          <el-form-item label="会员姓名" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="会员生日" prop="birthday">
            <el-date-picker
              v-model="ruleForm.birthday"
              type="date"
              placeholder="出生日期"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="会员电话" prop="phone">
            <el-input v-model="ruleForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="开卡金额" prop="money">
            <el-input v-model="ruleForm.money"></el-input>
          </el-form-item>
          <el-form-item label="可用积分" prop="integral">
            <el-input v-model="ruleForm.integral"></el-input>
          </el-form-item>
          <el-form-item label="支付类型" prop="payType">
            <el-select v-model="ruleForm.payType" placeholder="支付类型">
              <el-option
                v-for="(item, index) in payType"
                :key="index"
                :label="item.name"
                :value="item.type"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="会员地址" prop="address">
            <el-input type="textarea" v-model="ruleForm.address"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resettingDia('ruleForm')">取 消</el-button>
        <el-button type="primary" v-if="title == '新增会员'" @click="addMember"
          >确 定</el-button
        >
        <el-button type="primary" v-if="title == '编辑会员'" @click="editMember"
          >编 辑</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import memberApi from "../../api/member";

let payType = [
  {
    type: "1",
    name: "现金",
  },
  {
    type: "2",
    name: "微信",
  },
  {
    type: "3",
    name: "支付宝",
  },
  {
    type: "4",
    name: "银行卡",
  },
];
export default {
  data() {
    return {
      tableData: {
        id: "",
        cardNum: "",
        name: "",
        payType: "",
        birthday: "",
      },
      page: 1,
      size: 7,
      total: 0,
      memberList: [],
      payType: payType,
      dialogVisible: false,
      title: "",
      ruleForm: {
        cardNum: "",
        name: "",
        birthday: "",
        phone: "",
        money: "",
        integral: "",
        payType: "",
        address: "",
      },
      rules: {
        cardNum: [
          { required: true, message: "请输入会员卡号", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入会员姓名", trigger: "blur" }],
        birthday: [
          { required: true, message: "请输入会员生日", trigger: "change" },
        ],
        phone: [{ required: true, message: "请输入会员电话", trigger: "blur" }],
        money: [{ required: true, message: "请输入开卡金额", trigger: "blur" }],
        integral: [
          { required: true, message: "请输入可用积分", trigger: "blur" },
        ],
        payType: [
          { required: true, message: "请输入支付类型", trigger: "change" },
        ],
        address: [
          { required: true, message: "请输入会员地址", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getMemberList();
  },
  mounted() {},
  filters: {
    filterType(val) {
      let data = payType.find((item, index) => {
        return item.type == val;
      });
      return data.name;
    },
  },
  methods: {
    async getMemberList() {
      try {
        let res = await memberApi.getMemberList(
          this.page,
          this.size,
          this.tableData
        );
        if (res.data.flag) {
          this.memberList = res.data.data.rows;
          this.total = res.data.data.total;
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    handleSizeChange(val) {
      this.size = val;
      this.getMemberList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getMemberList();
    },
    search() {
      this.getMemberList();
    },
    resetting(formName) {
      this.$refs[formName].resetFields();
    },
    addDialog() {
      this.title = "新增会员";
      this.dialogVisible = true;
    },
    resettingDia(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    async addMember() {
      try {
        let res = await memberApi.addMember(this.ruleForm);
        if (res.data.flag) {
          this.$message.success(res.data.message);
          this.getMemberList();
          this.$refs["ruleForm"].resetFields();
          this.dialogVisible = false;
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    async getDialog(id) {
      this.title = "编辑会员";
      this.dialogVisible = true;
      try {
        let res = await memberApi.searchMember(id);
        if (res.data.flag) {
          this.ruleForm = res.data.data.rows[0];
          this.ruleForm.birthday = new Date(res.data.data.rows[0].birthday);
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    async editMember() {
      try {
        let res = await memberApi.editMember(this.ruleForm.id, this.ruleForm);
        if (res.data.flag) {
          this.$message.success(res.data.message);
          this.getMemberList();
          this.$refs["ruleForm"].resetFields();
          this.dialogVisible = false;
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    async delMember(id) {
      try {
        let res = await memberApi.delMember(id);
        if (res.data.flag) {
          this.$message.success(res.data.message);
          this.getMemberList();
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    }
  },
};
</script>

<style scoped lang="scss">
.el-form {
  margin-top: 20px;
}
.el-pagination {
  margin-top: 10px;
}
</style>
